// pages/me/withdrawal/index.js
import utils from '../../../utils/utils';
Page({
  /**
   * 页面的初始数据
   */
  coverRatio:10000,
  data: {
    coverMoney:0,
    coverAllMoney:0,
    pean:'',
    userInfo:{
      common:{
        pea_rate:10000,
        pea_blance:0
      }
    }
  },
  initPage:function () {
    let self = this;
    self.getUserInfo(function (userInfo) {
      let coverAllMoney;
      self.coverRatio = userInfo.common.pea_rate;
      coverAllMoney   = utils.fixFloat(userInfo.common.pea_blance / self.coverRatio);
      self.setData({
        coverAllMoney:coverAllMoney,
        userInfo:userInfo
      });
    },false,true);
  },
  changeInput:function(e){
    let userPea  = this.data.userInfo.common.pea_blance;
    let inputPea = utils.fixInt(e.detail.value);
    if(inputPea > userPea){
      inputPea = userPea;
    }
    if(inputPea === 0){
      inputPea = '';
    }
    this.setData({
      pean:inputPea
    });
    this.coverMoney();
  },
  coverMoney:function () {
    let pea;
    pea = this.data.pean / this.coverRatio;
    this.setData({
      coverMoney:pea
    });
  },
  toWithDraw:function () {
    if(this.data.pean <= 0){
      utils.msg('请输入要兑换的幸运豆数量！');
      return false;
    }
    if(this.data.pean < this.coverRatio){
      utils.msg('单笔兑换的幸运豆数量不能低于' + this.coverRatio);
      return false;
    }

    if((this.data.pean % this.coverRatio) > 0){
      utils.msg('兑换的幸运豆数量必须是' + this.coverRatio +'的整数倍！');
      return false;
    }

    utils.request({
      url:'/s/userinfo/PeaCashCard',
      data:{
        pea_count:this.data.pean
      },
      loading:true,
      callback:function (result) {
        if(result.status === 'ok'){
          setTimeout(function () {
            utils.toPage('/pages/me/notify/index?type=1',false,true);
          },100);
        }else{
          setTimeout(function () {
            utils.toPage('/pages/me/notify/index?type=2&msg=' + result.msg,false,true);
          },100);
        }
      }
    })
  }
});
